package classes
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Image;
	import mx.core.BitmapAsset;
	
	import qs.controls.SuperImage;
	
	public class PhotoObject
	{
		public var _index:Number;
		public var _title:String;		
		[Bindable] public var _bitmapData:BitmapData;
		[Bindable] public var _image:SuperImage;
		public var _transformsCollection:ArrayCollection = new ArrayCollection();
		public var _url:String;
		public var _bitmap:Bitmap = new Bitmap();	
		public function PhotoObject()
		{			
		}
		public function makeImage():SuperImage
		{
			    
                var bitmap:Bitmap = new Bitmap(_bitmapData);
                var img:SuperImage = new SuperImage()
                img.source = _bitmap;
                return img;

		}		
		public function rotateLeft():void 
		{				
			var m:Matrix = new Matrix();
			var q:Number  = 90 * Math.PI / 180 // 30 degrees in radians
			
			// rotate around the center of the image
			
			var centerX:Number = _image.width / 2;				
			var centerY:Number = _image.height /2;				
			m.translate(-1 * centerX, -1 * centerY);				
			m.rotate(q);				
			m.translate(centerX, centerY);
			_image.transform.matrix = m;	
			_transformsCollection.addItem(m);
				
		}
		public function rotateRight():void 
		{				
			var m:Matrix = new Matrix();
			var q:Number  = -90 * Math.PI / 180 // 30 degrees in radians

			// rotate around the center of the image
			
			var centerX:Number = _image.width / 2;				
			var centerY:Number = _image.height /2;				
			m.translate(-1 * centerX, -1 * centerY);				
			m.rotate(q);				
			m.translate(centerX, centerY);
			_image.transform.matrix = m;	
			_transformsCollection.addItem(m);							
		}
		
		public function set index(value:uint):void
		{
			_index = value;
		}
		public function get index():uint
		{
			return _index;
		}
		
		public function set title(value:String):void
		{
			_title = value;
		}
		public function get title():String
		{
			return _title;
		}
		
		public function setBitmapData(value:Image):void
		{
			var image:Image= Image(value)
 
		   if(image.width > 0)
		   {
		    	var bitmapData:BitmapData
		    	_bitmapData=new BitmapData(image.width,image.height)
		   		var matrix:Matrix = new Matrix()
		    	_bitmapData.draw(image,matrix)
		    	
		    	trace ("Bitmap data set using "+value.source+" ");		
		   } 
			
		}
		public function get bitmapData():BitmapData
		{
			for each (var item:Matrix in _transformsCollection)
			{
				var newWidth : uint = _bitmapData.width;
				var newHeight : uint = _bitmapData.height;
				_bitmapData = new BitmapData(newWidth, newHeight); 
						 				
				_bitmapData.draw(_bitmapData, item);	
			}
			return _bitmapData;
		}
		
		public function set image(value:SuperImage):void
		{
			for each (var item:Matrix in _transformsCollection)
			{
				var newWidth : uint = value.width;
				var newHeight : uint = value.height;
				_bitmapData = new BitmapData(newWidth, newHeight); 
						 				
				_bitmapData.draw(value, item);	
			}
			
			_image = new SuperImage();
			_image.source = new BitmapAsset(_bitmapData);
			
		}
		public function get image():SuperImage
		{
			for each (var item:Matrix in _transformsCollection)
			{
				var newWidth : uint = _bitmapData.width;
				var newHeight : uint = _bitmapData.height;
				_bitmapData = new BitmapData(newWidth, newHeight); 
						 				
				_bitmapData.draw(_bitmapData, item);	
			}
			
			_image = new SuperImage();
			_image.source = new BitmapAsset(_bitmapData);
			return _image;
		}
		
		public function set transformsCollection(value:ArrayCollection):void
		{
			_transformsCollection = value;
		}
		public function get tranformsCollection():ArrayCollection
		{
			return _transformsCollection;
		}
		public function addTransform(value:Matrix):void
		{
			if(!_transformsCollection.contains(value))
			{
				_transformsCollection.addItem(value);
			}
		} 
		public function set url(value:String):void
		{
			_url = value;
		}
		public function get url():String
		{
			return _url;
		}
	}
}